package org.phoenixframework.api.rbac.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.phoenixframework.api.rbac.service.ISysRoleService;
import org.phoenixframework.common.entity.SysRole;
import org.phoenixframework.common.entity.base.BaseEntity;
import org.phoenixframework.common.tools.ResultModel;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author doublelife
 * @since 2021-05-08
 */
@Slf4j
@Api(tags = {"系统角色"})
@AllArgsConstructor
@RestController
@RequestMapping("/sysRole")
public class SysRoleController extends BaseController {

    private final ISysRoleService sysRoleService;

    @ApiOperation("获取分页列表")
    @GetMapping("/findPage/{current}/{size}")
    public ResultModel<?> findPage(@ApiParam(name = "current", value = "当前页码", required = true)
                                        @PathVariable Long current,
                                        @ApiParam(name = "size", value = "每页记录数", required = true)
                                        @PathVariable Long size,
                                        @ApiParam(name = "sysRoleQueryVo", value = "查询对象", required = false)
                                                SysRole sysRoleQueryVo) {
        Page<SysRole> sysRolePage = new Page<>(current, size);
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(sysRoleQueryVo.getName()), SysRole::getName, sysRoleQueryVo.getName());
        wrapper.orderByDesc(BaseEntity::getCreatedDate);
        Page<SysRole> page = sysRoleService.page(sysRolePage, wrapper);
        return this.buildByPage(page);
    }

    @ApiOperation("保存信息")
    @PostMapping("/save")
    public ResultModel<?> save(@ApiParam(name = "sysRole", value = "对象信息", required = true) @Validated @RequestBody SysRole sysRole) {
        boolean result = sysRoleService.save(sysRole);
        return this.buildByBool(result);
    }

    @ApiOperation("根据id获取一条信息")
    @GetMapping("/find/{id}")
    public ResultModel<?> findById(@ApiParam(name = "id", value = "对象信息id", required = true) @PathVariable Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        return this.buildByObject(sysRole);
    }

    @ApiOperation("根据id修改信息")
    @PutMapping("/update")
    public ResultModel<?> updateById(@ApiParam(name = "sysRole", value = "对象信息", required = true) @Validated @RequestBody SysRole sysRole) {
        if (StringUtils.isBlank(sysRole.getId().toString())) {
            return ResultModel.error();
        }
        boolean result = sysRoleService.updateById(sysRole);
        return this.buildByBool(result);
    }

    @ApiOperation("根据id删除一条信息")
    @DeleteMapping("/remove/{id}")
    public ResultModel<?> removeById(@ApiParam(name = "id", value = "对象信息id", required = true) @PathVariable Long id) {
        boolean b = sysRoleService.removeById(id);
        return this.buildByBool(b);
    }

    @ApiOperation("根据id列表批量删除")
    @DeleteMapping("/removeByIds")
    public ResultModel<?> removeByIds(@RequestBody List<String> ids) {
        boolean b = sysRoleService.removeByIds(ids);
        return this.buildByBool(b);
    }

}
